package com.ck.util;

import com.ck.entity.HtmlEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class HtmlUtil {

    public static void main(String[] args) {
        List<HtmlEntity> list = new ArrayList<>();
        try {
            Document document = Jsoup.parse(new File("D:/Download/bookmarks_qq_2019_12_18.html"), "utf-8");
            //System.out.println(document.body().children());
            Elements docDL = document.select("DL").get(1).children();
            parseNodes(docDL,list,"1",false);
            //System.out.println(document.select("DL").get(1).children());
            //Elements docDL = document.select("DL").get(1).getAllElements();
            //parseElements(docDL,list,"1",false);
            System.out.println(list.size());
            System.out.println(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void parseNodes(Elements elements,List<HtmlEntity> list,String pid,boolean flag){
        if(flag){
            System.out.println(flag);
        }
        for(Element node:elements){
            System.out.println("ahref:"+node.select("a[href]"));
            List<Node> nodeList= node.childNodes();
            if(nodeList.size()>0){
                String uuid = UUID.randomUUID().toString().replace("-", "");
                for(Node n:nodeList){
                    if("h3".equalsIgnoreCase(n.nodeName())){
                        HtmlEntity tag = new HtmlEntity(uuid,"1",n.childNodes().get(0).toString(),0,"","");
                        list.add(tag);
                    }
                    if("dl".equalsIgnoreCase(n.nodeName())){
                        parseNodesList(n.childNodes(),list,uuid,false);
                        System.out.println(n);
                    }
                    if("a".equalsIgnoreCase(n.nodeName())){
                        HtmlEntity tag = new HtmlEntity("",pid,n.childNodes().get(0).toString(),1,n.attr("href"),n.attr("icon"));
                        list.add(tag);
                    }

                }
            }

            System.out.println(nodeList);
        }
    }

    private static void parseNodesList(List<Node> arrList,List<HtmlEntity> list,String pid,boolean flag){
        if(flag){
           arrList = new ArrayList(arrList);
           if(arrList.size()>1){
               arrList.remove(0);
           }
        }
        List<Node> node = new ArrayList<>(arrList);
        for(Node n:node){
            List<Node> childList = n.childNodes();
            for(Node child:childList){
                if("h3".equalsIgnoreCase(child.nodeName())){
                    HtmlEntity tag = new HtmlEntity(pid,"1",child.childNode(0).toString(),0,"","");
                    list.add(tag);
                    parseNodesList(n.childNodes(),list,pid,true);
                }
                if("a".equalsIgnoreCase(child.nodeName())){
                    HtmlEntity tag = new HtmlEntity("",pid,child.childNode(0).toString(),1,child.childNode(0).attr("href"),child.childNode(0).attr("icon"));
                    list.add(tag);
                }
                if(flag && "dt".equalsIgnoreCase(child.nodeName())){
                    parseNodesList(child.childNodes(),list,pid,true);
                }
            }

        }
    }

    private static void parseElements(Elements elements,List<HtmlEntity> list,String pid,boolean flag){
        HtmlEntity htmlEntity;
        boolean aFlag;
        for(Element node:elements){
            htmlEntity = new HtmlEntity("","","",0,"","");
            String uuid =UUID.randomUUID().toString().replace("-", "");
            if(node.tagName().equalsIgnoreCase("h3")){
                htmlEntity.setId(uuid);
                htmlEntity.setType(0);
                htmlEntity.setName(node.text());
                if(node.nextElementSibling() != null && "dl".equalsIgnoreCase(node.nextElementSibling().tagName())){
                    //System.out.println("我是"+node.text()+"我有孩子");
                    //System.out.println("我是下一个元素:"+node.nextElementSibling().children());
                    parseElements(node.nextElementSibling().children(), list,uuid,true);
                    //System.out.println("我是孩子"+childHtmlEntity);
                }
            }

            if(flag){
                Elements links = node.select("a[href]");
                aFlag = links.size()==1;
            }else{
                aFlag = node.tagName().equalsIgnoreCase("a");
            }
            if(aFlag){
                htmlEntity.setId(uuid);
                htmlEntity.setType(1);
                htmlEntity.setName(node.text());
                htmlEntity.setHref(node.attr("HREF"));
                htmlEntity.setIcon(node.attr("ICON"));
            }
            if(isObjectFieldNotEmpty(htmlEntity)){
                htmlEntity.setPid(pid);
                list.add(htmlEntity);
            }
        }
    }

    public static boolean isObjectFieldNotEmpty(Object obj) {
        Class<?> clazz=obj.getClass();  //得到类对象
        Field[] fs=clazz.getDeclaredFields(); //得到属性集合
        try {
            for(Field field:fs){            //遍历属性
                field.setAccessible(true); //设置属性是可以访问的（私有的也可以）
                if(field.get(obj)!=null && field.get(obj)!="" && !"null".equalsIgnoreCase((String)field.get(obj))){
                    return true;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }
}
